<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessageBox, ElMessage, ElDialog } from 'element-plus'
import { selectSelfAllInfo } from '@/api/attendance'
const userStore = useUserStore()
const tableData = ref([])
const showReasonDialog = ref(false)
const currentRecordId = ref(null)
const currentReason = ref('')

// 状态文本和颜色映射
const statusTextMap = {
  1: '待审批',
  2: '已批准',
  3: '已驳回',
}

const statusColorMap = {
  1: '#E6A23C', // 黄色（待审批）
  2: '#67C23A', // 绿色（已批准）
  3: '#F56C6C', // 红色（已拒绝）
}

const getStatusText = (status) => {
  return statusTextMap[status] || '未知状态'
}

const getStatusColor = (status) => {
  return statusColorMap[status] || '#303133' // 默认黑色
}

// 显示驳回理由对话框
const showReason = (record) => {
  currentRecordId.value = record.id
  currentReason.value = record.reason || '无理由'
  showReasonDialog.value = true
}

// 获取岗位信息数据
const fetchPositionInfo = async () => {
  try {
    const res = await selectSelfAllInfo({
      name: userStore.user.name // 根据用户 ID 获取岗位信息
    })
    if (res && res.data) {
      tableData.value = res.data.data 
    } else {
      tableData.value = []
    }
  } catch (error) {
    console.error('获取岗位信息失败:', error)
    tableData.value = []
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchPositionInfo()
})
</script>

<template>
  <el-card class="info-card">
    <el-button type="primary" class="info-button">
      我的调岗信息
    </el-button>
  </el-card>

  <el-card class="table-card" style="margin-top: 20px;">
    <el-table :data="tableData" style="width: 100%" height="600">
      <!-- 序号 -->
      <el-table-column type="index" label="序号" width="80" align="center" />

      <!-- 姓名 -->
      <el-table-column prop="name" label="姓名" width="120" align="center" />

      <!-- 调岗前 -->
      <el-table-column prop="typeDe" label="调岗前" width="180" align="center" />

      <!-- 调岗后 -->
      <el-table-column prop="endDe" label="调岗后" width="180" align="center" />

      <!-- 调岗内容 -->
      <el-table-column prop="content" label="调岗内容" align="center">
        <template #default="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>

      <!-- 状态 -->
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template #default="scope">
          <span :style="{ color: getStatusColor(scope.row.status) }">
            {{ getStatusText(scope.row.status) }}
          </span>
        </template>
      </el-table-column>

      <!-- 标注 -->
      <el-table-column label="标注" width="150" align="center">
        <template #default="scope">
          <el-button @click="showReason(scope.row)">标注</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 查看驳回理由对话框 -->
  <el-dialog title="驳回理由" visible.sync="showReasonDialog">
    <div>{{ currentReason }}</div>
    <span class="dialog-footer">
      <el-button @click="showReasonDialog = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<style scoped>
.info-card {
  height: 70px;
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.info-button {
  width: 200px;
  font-size: 16px;
  border-radius: 8px;
  text-align: center;
  background: linear-gradient(to right, #4a6bff, #7986cb);
  border: none;
  transition: all 0.3s;
}

.info-button:hover {
  background: linear-gradient(to right, #7986cb, #4a6bff);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.info-button:active {
  transform: translateY(0);
}

.table-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
}
</style>